package cc.lucien.service;

import cc.lucien.dao.UserDao;
import cc.lucien.entity.User;
import cc.lucien.utils.DBHelper;

import javax.servlet.http.Part;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserService {

    UserDao userDao = new UserDao();

    /**
     * 查找所有用户
     * @return
     */
    public List<User> findAll() {
        List<User> list = new ArrayList<>();
        User temp = null;
        ResultSet resultSet = userDao.findAll();
        try {
            while (resultSet.next()){
                temp = getUser(resultSet);
                list.add(temp);
            }
        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            DBHelper.close();
        }
        return list;
    }

    /**
     * 根据用户名删除指定用户
     * @param username
     */
    public void deleteByUsername(String username) {
        //删除头像
        String fileName = null;
        ResultSet resultSet = userDao.findByUsername(username);
        try {
            if (resultSet.next())
                fileName = resultSet.getString("imgurl");
            if (fileName != null){
                File file = new File("F:\\photo\\"+fileName);
                if (file.exists())
                    file.delete();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        userDao.deleteByUsername(username);
    }

    public void add(String username, String password, String realname, Part part, String email, Long phone, String source) {
        //保存图片并获取路径
        String imgurl = null;
        if (part.getContentType().equalsIgnoreCase("image/png") ||
                part.getContentType().equalsIgnoreCase("image/jpeg"))
            imgurl = saveImg(username, part);
        Object[] objects = {
                username,password,realname,imgurl,email,phone,source
        };
        userDao.add(objects);
    }

    private String saveImg(String username, Part part) {
        String path = null;
        String fileName = null;
        if (part != null) {
            fileName = username + "." + part.getContentType().split("/")[1];
            path = "F:\\photo\\" + fileName;
            //写文件
            try {
                part.write(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileName;
    }

    public User findByUsername(String username) {
        User user = null;
        ResultSet resultSet = userDao.findByUsername(username);
        try {
            while (resultSet.next()){
                user = getUser(resultSet);
            }
        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            DBHelper.close();
        }
        return user;
    }

    public void update(String username, String password, String realname, Part part,String currentImg, String email, Long phone, String source) {
        //获取图片的保存路径
        String imgurl = null;
        if (!part.getContentType().equalsIgnoreCase("image/png") && !part.getContentType().equalsIgnoreCase("image/jpeg")
                && !part.getContentType().equalsIgnoreCase("image/jpg"))
            imgurl = currentImg;
        else
            imgurl = saveImg(username, part);
        int i = userDao.update(username,password,realname,imgurl,email,phone,source);
    }

    private User getUser(ResultSet resultSet) throws SQLException{
        User user;
        String username = resultSet.getString("username");
        String password = resultSet.getString("password");
        String realname = resultSet.getString("realname");
        String imgurl = resultSet.getString("imgurl");
        String email = resultSet.getString("email");
        Long phone = resultSet.getLong("phone");
        String source = resultSet.getString("source");
        user = new User(username,password,realname,imgurl,email,phone,source);
        return user;
    }
}
